package com.daqsoft.service.system;

import com.daqsoft.dao.UserDao;
import com.daqsoft.data.Group;
import com.daqsoft.data.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户管理服务层
 * @author 周麟东 on 16:31
 */
@Service
public class UserManageService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private GroupService groupService;

    public User save(User user , int groupId , int pid){
        Group group =  groupService.findOne(groupId);
        user.setUserGroup(group);
        if (pid != -1){
            user.setpUser(userDao.findOne(pid));
        }
        user = userDao.save(user);
        return user;
    }

    public void remove(int id) {
        userDao.delete(id);
    }

    public List<User> getAllUsers() {
        return userDao.findAll();
    }

    public User findOne(int id) {
        return userDao.findOne(id);
    }

    public List<User> getUsersByGroupId(int groupId) {
        return userDao.findByUserGroup(groupService.findOne(groupId));
    }
}
